<?php

namespace App\Http\Controllers\Api\Users;

use App\Http\Model\ResponseResult;
use App\Models\Users\Role;
use DB;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class RoleController extends Controller
{
    public function __construct()
    {
        $this->middleware(['api-auth', 'permission:admin-role']);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return response(ResponseResult::success(Role::orderBy('id')->get()));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return void
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return array
     */
    public function store(Request $request)
    {
        $rules = [
            'name' => 'required|unique:roles,name|max:255',
            'display_name' => 'max:255',
            'description' => 'max:255',
            'can_delete' => 'boolean',
            'is_default' => 'boolean',
            'flag' => 'integer|max:60'
        ];
        $this->validator($request, $rules);

        $data = $request->only(array_keys($rules));

        $role = Role::create($data);
        if (!empty($role)) {
            return ResponseResult::success(__('app.create.success'));
        }
        return ResponseResult::error(1000, __('app.create.fail'));
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Users\Role $role
     * @return array
     */
    public function show(Role $role)
    {
        $role->perms;
        return ResponseResult::success($role);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Users\Role $role
     * @return \Illuminate\Http\Response
     */
    public function edit(Role $role)
    {
        //TODO
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \App\Models\Users\Role $role
     * @return array
     */
    public function update(Request $request, Role $role)
    {
        if ($role->checkFlagByUser(\Auth::user()) === false) {
            return ResponseResult::error(2000, __('app.unauthorized'));
        }

        $rules = [
            'display_name' => 'max:255',
            'description' => 'max:255',
            'flag' => 'integer|max:60',
            'perms' => 'array'
        ];
        $this->validator($request, $rules);
        $data = $request->only(array_keys($rules));
        try {
            DB::beginTransaction();
            $role->deletePermission();
            $role->attachPermission($data['perms']);
            $role->update($data);
            DB::commit();
            return ResponseResult::success(__('app.update.success'));
        } catch (\Exception $e) {
            logger($e);
            DB::rollBack();
            return ResponseResult::error(1000, __('app.update.fail'));
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Users\Role $role
     * @return array
     */
    public function destroy(Role $role)
    {
        if ($role->checkFlagByUser(\Auth::user()) === false) {
            return ResponseResult::error(2000, __('app.unauthorized'));
        }

        if ($role->can_delete === false || $role->is_default === true) {
            return ResponseResult::error(2000, __('app.delete.not'));
        }
        if ($role->delete()) {
            return ResponseResult::success(__('app.delete.success'));
        }
        return ResponseResult::error(1000, __('app.delete.fail'));
    }
}
